package com.infore.qualityControl.select.filter.condition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.infore.qualityControl.select.chain.ConditionFilterChain;
import com.infore.qualityControl.select.enums.OperatorEnum;
import com.infore.qualityControl.select.filter.ConditionFilter;
import com.infore.qualityControl.select.util.QueryUtil;

/**
 * 区间过滤器
 */
public class BetweenFilter implements ConditionFilter {

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void doFilter(List<Predicate> predicates,CriteriaBuilder criteriaBuilder,Root<?> root,
			String property,String operator,String content,ConditionFilterChain chain,boolean interrupt) {
		if(OperatorEnum.BETWEEN.getOperator().equals(operator)) {
			Path path = QueryUtil.getPropertyPath(property, root);
			String[] contents = content.split(",");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				if (contents[0].contains("-") || contents[0].contains(":")) { //代表时间区间
					Date sdate = sdf.parse(contents[0]);
					Date ddate = sdf.parse(contents[1]);
					predicates.add(criteriaBuilder.between(path, sdate, ddate));
				} else { //数值区间
					predicates.add(criteriaBuilder.between(path, Float.valueOf(contents[0]), Float.valueOf(contents[1])));
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			interrupt = true;
		}
		chain.doFilter(predicates,criteriaBuilder,root,property,operator,content,chain,interrupt);
	}

}
